﻿String.format = function() {
    var s = arguments[0];
    for (var i = 0; i < arguments.length - 1; i++) {
        var reg = new RegExp("\\{" + i + "\\}", "gm");
        s = s.replace(reg, arguments[i + 1]);
    }

    return s;
};
String.prototype.endsWith = function(suffix) {
    return (this.substr(this.length - suffix.length) === suffix);
};

String.prototype.startsWith = function(prefix) {
    return (this.substr(0, prefix.length) === prefix);
};

function replaceNewButtonText(e, t) {
    e.find(".t-grid-add").first().text(t).prepend("<span class='t-icon t-add'>");
}

function replaceDeleteConfirmation(e, q, p) {
    var grid = $(e).data('tGrid');
    $(e).find(".t-grid-delete").click(function () {
        grid.localization.deleteConfirmation = String.format(q, p);
    });
}



function replaceEditPopupTitle(e,it,ut) {
    var popup = $("#" + e.currentTarget.id + "PopUp");
    var popupDataWin = popup.data("tWindow");
    popupDataWin.center();

    if (e.mode == "insert")
        popupDataWin.title(it);
    else
        popupDataWin.title(ut);

    // Hide fields from the pop-up.
    //$(e.form).find("#CustomerId").closest(".editor-field").prev().andSelf().hide();
    //$(e.form).find("#MiddleInitial").closest(".editor-field").prev().andSelf().hide();
}


function hideEditPopupField(e, f) {
    //$(e.form).find("#" + f).closest(".editor-field").prev().andSelf().hide();
    $(e.form).find("#" + f).parent().parent().parent().andSelf().hide();
}

var addCodePopup11;
function addCode() {
    var editor = $('#ContentHtml').data('tEditor');
    if (!addCodePopup11) {
        addCodePopup11 =
                            $('<div class="html-view">' +
                                  '<div class="textarea t-state-default"><textarea></textarea></div>' +
                                  '<div class="t-button-wrapper">' +
                                        '<select id="selecLang123">' +
                                            '<option value="cs">CS</option>' +
                                            '<option value="vb">VB</option>' +
                                            '<option value="xml">XML</option>' +
                                            '<option value="sql">SQL</option>' +
                                            '<option value="html">HTML</option>' +
                                            '<option value="js">JScript</option>' +
                                            '<option value="css">CSS</option>' +
                                            '<option value="none">NONE</option>' +
                                        '</select>' +
                                      '<button id="htmlCancel123" class="t-button t-state-default">Cancel</button>' +
                                      '<button id="htmlUpdate123" class="t-button t-state-default">Update</button>' +
                                  '</div>' +
                              '</div>')
                            .css('display', 'none')
                          .tWindow({
                              title: 'Add Code',
                              modal: true,
                              resizable: false,
                              draggable: true,
                              width: 700,
                              onLoad: function () {
                                  var $popup = $(this);
                                  $popup.find('.textarea')
                                            .css('width', function () {
                                                return 700 - (this.offsetWidth - $(this).width());
                                            })
                                            .focus()
                                          .end()
                                          .find('#htmlCancel123')
                                            .click(function () {
                                                addCodePopup11.close();
                                            })
                                          .end()
                                              .find('#htmlUpdate123')
                                                .click(function () {
                                                    var code = $popup.find('textarea').val();
                                                    var lang = $popup.find('#selecLang123').val();
                                                    var html = code;
                                                    if (lang != 'none')
                                                        html = '<p><b>[' + lang + ']</b></p><pre class="prettyprint" lang="' + lang + '">' + encode(code) + '</pre><p></p>';
                                                    editor.exec('insertHtml', { value: html });
                                                    addCodePopup11.close();
                                                });
                              },
                              onClose: function () {
                                  editor.focus();
                              },
                              effects: $.telerik.fx.toggle.defaults()
                          })
                        .data('tWindow');
    }
    addCodePopup11.center().open();
}
function encode(input) {
    var result = "";
    for (var i = 0; i < input.length; i++) {
        result = result + input[i]
            .replace(/&/g, '&amp;')
            .replace(/"/g, '&quot;')
            .replace(/'/g, '&#39;')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;')
            .replace(/ /g, '&nbsp;') 
            .replace(/\t/g, '&#09;')
            .replace(/\n/g, '<br>');
    }
    return result; 
}

/* google +1 */
window.___gcfg = { lang: 'tr' };
(function () {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();

/* google analytics */
var _gaq = _gaq || [];
_gaq.push(['_setAccount', googleAnalyticsId]);
_gaq.push(['_trackPageview']);

(function () {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

/* facebook */
(function (d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) { return; }
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/tr_TR/all.js#xfbml=1";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

/* twitter search */
$(document).ready(function () {
    var twitter_api_url = 'http://search.twitter.com/search.json';
    $.ajaxSetup({ cache: true });

    if (twitter_user != '')
        $.getJSON(
            twitter_api_url + '?callback=?&rpp=1&q=from:' + twitter_user,
            function (data) {
                $.each(data.results, function (i, tweet) {
                    if (tweet.text !== undefined) {
                        var tweet_html = '<div class="tweet_text">';
                        tweet_html += '<a href="http://www.twitter.com/';
                        tweet_html += twitter_user + '/status/' + tweet.id + '">';
                        tweet_html += tweet.text + '<\/a><\/div>';

                        $('#tweet_container').append(tweet_html);
                    }
                });
            });
});